package cls.elmp.atf.utils;

/**
 * Created by IntelliJ IDEA.
 * User: luotao
 * Date: 12-5-5
 * Time: 上午12:06
 * To change this template use File | Settings | File Templates.
 */

import com.service.atf.base.core.*;
import com.service.atf.base.exception.TFException;
import com.service.atf.base.model.BaseHeader;
import com.service.atf.base.type.ResultReplyRead;
import com.service.atf.base.utils.ATFConstants;
import com.service.atf.base.utils.ATFLogger;
import com.service.atf.reply.ac.AccountManager;
import com.service.atf.reply.ac.ObjectFactory;
import com.service.atf.reply.core.IReadExecutor;
import com.service.atf.reply.service.TFCheckpointService;
import com.service.atf.reply.service.TFEndService;
import com.service.atf.reply.service.TFStartService;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * User: luotao
 * Date: 12-3-21
 * Time: 09:23:00
 */
public class TFReadProcess implements IReadProcess {
    IReadExecutor start = new TFStartService();
    IReadExecutor checkPoint = new TFCheckpointService();
    IReadExecutor end = new TFEndService();
    Map<String, IReadExecutor> executorMap = new HashMap<String, IReadExecutor>();

    public void addExecutor(String key, IReadExecutor iReadExecutor) {
        executorMap.put(key, iReadExecutor);
    }

    public void setExecutorMap(Map<String, IReadExecutor> executorMap) {
        this.executorMap = executorMap;
    }

    @Override
    public ResultReplyRead doRead(ITFInfo itfInfo, int offset, byte[] data) {
        ResultReplyRead result = new ResultReplyRead(0);
        Map out = new LinkedHashMap();
        ByteConvertion convertion = null;
        boolean isNotyfy = false;
        try {
            String type = BaseHeader.getTypeAndTradeType(data);
            convertion = findByteConvertion(type);
            convertion.byte2Object(data);
            out.put("convertion.getThreadid()", convertion.getThreadid());
            out.put(GlobalDefine.ATF_MAPKEY_ERRCODE, 0);
            out.put(GlobalDefine.ATF_MAPKEY_ERRCODE, 0);
            IReadExecutor iReadExecutor = executorMap.get(type);
            if (iReadExecutor != null) {
                isNotyfy = iReadExecutor.isNotify();
                iReadExecutor.initOut(out);
                iReadExecutor.execute(itfInfo.isRecover(), AccountManager.getInstance(), convertion, out);
            }
            return result;
        } catch (Throwable e) {
            if (e instanceof TFException) {
                ATFLogger.syserr(e.toString());
                out.put(GlobalDefine.ATF_MAPKEY_ERRCODE, ((TFException) e).getErrCode());
                result.setCode(((TFException) e).getErrCode());
                return result;
            } else if (e instanceof RuntimeException) {
                e.printStackTrace();
                ATFLogger.syserr(e.toString());
                out.put(GlobalDefine.ATF_MAPKEY_ERRCODE, ATFConstants.ATF_ERR);
                result.setCode(ATFConstants.ATF_ERR);
                return result;
            } else if (e instanceof Exception) {
                e.printStackTrace();
                ATFLogger.syserr(e.toString());
                out.put(GlobalDefine.ATF_MAPKEY_ERRCODE, ATFConstants.ATF_ERR);
                result.setCode(ATFConstants.ATF_ERR);
                return result;
            } else if (e instanceof Error) {
                e.printStackTrace();
                ATFLogger.syserr(e.toString());
                out.put(GlobalDefine.ATF_MAPKEY_ERRCODE, ATFConstants.ATF_ERR);
                result.setCode(ATFConstants.ATF_ERR);
                return result;
            } else {
                e.printStackTrace();
                ATFLogger.syserr(e.toString());
                out.put(GlobalDefine.ATF_MAPKEY_ERRCODE, ATFConstants.ATF_ERR);
                result.setCode(ATFConstants.ATF_ERR);
                return result;
            }
        } finally {
            ATFLogger.sysdebug("type :" + (char) convertion.getType() + (char) convertion.getTradeType() + " reply回写数据 : " + out);
            if (!itfInfo.isRecover() && isNotyfy) {
                responseResult(out, convertion);
            }
        }
    }


    public void responseResult(Map map, ByteConvertion convertion) {

    }

    public ByteConvertion findByteConvertion(String type) {
        if ("11".equals(type)) {
            return ObjectFactory.getTrade();
        } else if ("12".equals(type)) {
            return ObjectFactory.getCharge();
        } else if ("13".equals(type)) {
            return ObjectFactory.getDraw();
        } else if ("14".equals(type)) {
            return ObjectFactory.getBingo();
        } else if ("15".equals(type)) {
            return ObjectFactory.getTFLogin();
        } else if ("16".equals(type)) {
            return ObjectFactory.getLogout();
        } else if ("17".equals(type)) {
            return ObjectFactory.getTFReg();
        } else if ("18".equals(type)) {
            return ObjectFactory.getGameEnter();
        } else if ("19".equals(type)) {
            return ObjectFactory.getReplyGameExit();
        } else if ("1A".equals(type)) {
            return ObjectFactory.getGameComplate();
        } else if ("1B".equals(type)) {
            return ObjectFactory.getRefund();
        } else if ("21".equals(type)) {
            return ObjectFactory.getTfCheckpoint();
        } else if ("22".equals(type)) {
            return ObjectFactory.getTfStart();
        } else if ("23".equals(type)) {
            return ObjectFactory.getTfEnd();
        }
        return null;
    }
}

